<template>
  <div class="groupon-block" :style="containerStyle">
    <div class="groupon-header">
      <div class="groupon-title">
        <el-icon><Timer /></el-icon>
        <span>{{ data.title || '拼团活动' }}</span>
      </div>

      <div class="groupon-more" v-if="data.showMore">
        <span>更多</span>
        <el-icon><ArrowRight /></el-icon>
      </div>
    </div>

    <!-- 无商品 -->
    <div v-if="!data.grouponGoods || !data.grouponGoods.length" class="empty-container">
      <el-empty description="暂无拼团商品" />
    </div>

    <!-- 拼团商品列表 -->
    <div
      v-else
      class="groupon-goods-container"
      :style="goodsContainerStyle"
    >
      <div
        v-for="(goods, index) in data.grouponGoods"
        :key="index"
        class="groupon-goods-item"
        :style="goodsItemStyle"
      >
        <div class="goods-image-container">
          <img
            :src="goods.image"
            :alt="goods.title"
            class="goods-image"
          />
          <div class="groupon-tag">拼团</div>
        </div>

        <div class="goods-info">
          <h3 class="goods-title">{{ goods.title }}</h3>

          <div class="goods-price-container">
            <span class="goods-price">¥{{ goods.grouponPrice }}</span>
            <span class="goods-original-price">¥{{ goods.originalPrice }}</span>
            <span class="goods-discount">{{ goods.discount }}折</span>
          </div>

          <div class="goods-group-info">
            <span class="group-people">{{ goods.groupPeople }}人团</span>
            <span class="group-count">{{ goods.groupCount }}团已拼</span>
          </div>

          <div class="goods-progress" v-if="data.showProgress">
            <el-progress 
              :percentage="goods.progress" 
              :stroke-width="8"
              :show-text="false"
              color="#ff6034"
            />
            <div class="progress-text">已拼{{ goods.progress }}%</div>
          </div>

          <el-button 
            type="danger" 
            size="small" 
            class="groupon-button"
            @click="handleJoinGroup(goods)"
          >
            去拼团
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { Timer, ArrowRight } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 定义组件属性
const props = defineProps({
  data: {
    type: Object,
    default: () => ({})
  },
  styles: {
    type: Object,
    default: () => ({})
  }
})

// 计算容器样式
const containerStyle = computed(() => {
  return {
    marginTop: `${props.styles.marginTop || 0}px`,
    marginBottom: `${props.styles.marginBottom || 0}px`,
    marginLeft: `${props.styles.marginLeft || 0}px`,
    marginRight: `${props.styles.marginRight || 0}px`,
    backgroundColor: props.data.backgroundColor || '#ffffff',
    borderRadius: `${props.data.borderRadius || 0}px`
  }
})

// 计算商品容器样式
const goodsContainerStyle = computed(() => {
  return {
    display: 'grid',
    gridTemplateColumns: `repeat(${props.data.columns || 2}, 1fr)`,
    gap: `${props.data.margin || 10}px`,
    padding: `${props.data.padding || 10}px`
  }
})

// 计算商品项样式
const goodsItemStyle = computed(() => {
  return {
    borderRadius: `${props.data.itemBorderRadius || 4}px`,
    overflow: 'hidden',
    backgroundColor: props.data.itemBackgroundColor || '#ffffff'
  }
})

// 参加拼团
function handleJoinGroup(goods) {
  // 这里应该跳转到拼团页面
  ElMessage.success(`正在前往"${goods.title}"的拼团页面`)
}
</script>

<style lang="scss" scoped>
.groupon-block {
  width: 100%;

  .groupon-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 15px;

    .groupon-title {
      display: flex;
      align-items: center;
      font-size: 16px;
      font-weight: bold;
      color: #333;

      .el-icon {
        margin-right: 5px;
        color: #ff6034;
      }
    }

    .groupon-more {
      display: flex;
      align-items: center;
      font-size: 12px;
      color: #999;

      .el-icon {
        margin-left: 2px;
      }
    }
  }

  .empty-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 200px;
  }

  .groupon-goods-container {
    width: 100%;

    .groupon-goods-item {
      display: flex;
      flex-direction: column;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      transition: transform 0.3s;

      &:hover {
        transform: translateY(-3px);
      }

      .goods-image-container {
        width: 100%;
        padding-top: 100%;
        position: relative;
        overflow: hidden;

        .goods-image {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          object-fit: cover;
        }

        .groupon-tag {
          position: absolute;
          top: 10px;
          left: 10px;
          padding: 2px 6px;
          background-color: #ff6034;
          color: #fff;
          font-size: 12px;
          border-radius: 4px;
        }
      }

      .goods-info {
        padding: 10px;
        flex: 1;
        display: flex;
        flex-direction: column;

        .goods-title {
          margin: 0 0 8px 0;
          font-size: 14px;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .goods-price-container {
          display: flex;
          align-items: center;
          margin-bottom: 8px;

          .goods-price {
            font-size: 16px;
            font-weight: bold;
            color: #ff6034;
            margin-right: 5px;
          }

          .goods-original-price {
            font-size: 12px;
            color: #999;
            text-decoration: line-through;
            margin-right: 5px;
          }

          .goods-discount {
            font-size: 12px;
            color: #ff6034;
            background-color: rgba(255, 96, 52, 0.1);
            padding: 1px 4px;
            border-radius: 4px;
          }
        }

        .goods-group-info {
          display: flex;
          justify-content: space-between;
          margin-bottom: 8px;

          .group-people {
            font-size: 12px;
            color: #ff6034;
            font-weight: bold;
          }

          .group-count {
            font-size: 12px;
            color: #999;
          }
        }

        .goods-progress {
          margin-bottom: 8px;

          .progress-text {
            font-size: 12px;
            color: #ff6034;
            margin-bottom: 3px;
          }
        }

        .groupon-button {
          width: 100%;
          margin-top: auto;
        }
      }
    }
  }
}
</style>
